home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13169 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.4 KB  |  178 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: Linker error problem
  5. Date: 23 Mar 1996 23:18:39 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4j20sf$qnt@arl-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: ad53-232.compuserve.com
  9.  
  10. boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles) s'Θcrit :
  11. > I am getting a linker error as follows. Undefined symbol 
  12. > Address::ThisPtrList in module address.cpp.
  13. > I don't understand this non sense because it is defined
  14. > as a static class member and its value is initialized.
  15. > See $$$$ for definitions #### for causes of the error (if
  16. > I comment out these lines then the error disappears.
  17. > // address.h
  18. > #include <iostream.h>
  19. > #ifndef __ADDRESS_H
  20. > #define __ADDRESS_H
  21. > #define SIZE 100
  22. > #include "defines.h"
  23. > class Address
  24. > {
  25. >      private : int Number;
  26. >            char *Street;
  27. >            char *City;
  28. >            int Zip;
  29. >         $$$$$$   static Address *ThisPtrList[SIZE];
  30. -------------------------------------------------------
  31. Here you declare: Address * Address::ThisPtrList[SIZE]
  32. -------------------------------------------------------
  33. >            static unsigned int NumberInstances;
  34. >            static unsigned int InstanceNumber;
  35. >      public : // Constructors
  36. >           Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  37. >           Address();
  38. >           // Copy constructor
  39. >           Address(Address& AnAddress);
  40. >           // Deconstructor
  41. >           ~Address();
  42. >           // Operator overloads
  43. >           friend ostream& operator << (ostream& OutPutStream,Address& AnAddress);
  44. >           friend istream& operator >>(istream& InPutStream,Address& AnAddress);
  45. >           // Functions
  46. >           void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
  47. >           unsigned int Address::NumberOfInstances();
  48. >           void Address::DisplayAll();
  49. > };
  50. > #endif
  51. > // address.cpp
  52. > #include "address.h"
  53. > #include <string.h>
  54. > #include <iostream.h>
  55. > // Data initializations
  56. > unsigned int Address::NumberInstances=0;
  57. > unsigned int Address::InstanceNumber=0;
  58. > $$$$ Address Address::*ThisPtrList={0};
  59. ---------------------------------------------------
  60. The previous line is not correct. You only declare
  61. a pointer to a member of the class Address, which points
  62. to an object of type Address. This is valid even if there
  63. is no such member in the class Address !! (the compiler
  64. makes no assumption on WHICH member the pointer should
  65. point to, but only on the type of that member, so that
  66. dereferencing it thru an instance pointer could lead to
  67. a valid complete member reference.)
  68.  
  69. You should write:
  70. Address * Address::ThisPtrList[SIZE] = {0};
  71. ---------------------------------------------------
  72. > // Constructors
  73. > Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  74. > {
  75. >      Number=ANumber;
  76. >      Street=new char[strlen(AStreet)+1];
  77. >      strcpy(Street,AStreet);
  78. >      City=new char[strlen(ACity)+1];
  79. >      strcpy(City,ACity);
  80. >      Zip=AZip;
  81. > ####     ThisPtrList[NumberInstances]=this;
  82. ---------------------------------------------------
  83. With the previous correction, this line is correct
  84. ---------------------------------------------------
  85. >      NumberInstances++;
  86. >      InstanceNumber=NumberInstances;
  87. > }
  88. > Address::Address()
  89. > {
  90. >      Number=0;
  91. >      Street=new char[strlen("")+1];
  92. >      strcpy(Street,"");
  93. >      City=new char[strlen("")+1];
  94. >      strcpy(City,"");
  95. >      Zip=0;
  96. > ####     ThisPtrList[NumberInstances]=this;
  97. ---------------------------------------------------
  98. With the previous correction, this line is correct
  99. ---------------------------------------------------
  100. >      NumberInstances++;
  101. >      InstanceNumber=NumberInstances;
  102. > }
  103. > // Copy constructor
  104. > Address::Address(Address& AnAddress)
  105. > {
  106. >      Number=AnAddress.Number;
  107. >      Street=new char[strlen(AnAddress.Street)+1];
  108. >      strcpy(Street,AnAddress.Street);
  109. >      City=new char[strlen(AnAddress.City)+1];
  110. >      strcpy(City,AnAddress.City);
  111. >      Zip=AnAddress.Zip;
  112. > ####     ThisPtrList[NumberInstances]=this;
  113. ---------------------------------------------------
  114. With the previous correction, this line is correct
  115. ---------------------------------------------------
  116. >      NumberInstances++;
  117. >      InstanceNumber=NumberInstances;
  118. > }
  119. > // Deconstructor
  120. > Address::~Address()
  121. > {
  122. >      Number=0;
  123. >      delete Street;
  124. >      delete City;
  125. >      Zip=0;
  126. > }
  127. > // Operator overloads
  128. > ostream& operator <<(ostream& OutPutStream,Address& AnAddress)
  129. > {
  130. >      OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
  131. >      OutPutStream<<"City : "<<AnAddress.City<<EOLN;
  132. >      OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
  133. >      return(OutPutStream);
  134. > }
  135. > istream& operator >>(istream& InPutStream,Address& AnAddress)
  136. > {
  137. >      char *StreetName,*CityName,ch;
  138. >      StreetName=new char[50];
  139. >      CityName=new char[50];
  140. >      cout<<"Number : ";
  141. >      cin>>AnAddress.Number;
  142. >      GetEndOfLine();
  143. >      cout<<"Street : ";
  144. >      cin.getline(StreetName,49,EOLN);
  145. >      delete AnAddress.Street;
  146. >      AnAddress.Street=new char[strlen(StreetName)+1];
  147. >      strcpy(AnAddress.Street,StreetName);
  148. >      cout<<"City : ";
  149. >      cin.getline(CityName,49,EOLN);
  150. >      delete AnAddress.City;
  151. >      AnAddress.City=new char[strlen(CityName)+1];
  152. >      strcpy(AnAddress.City,CityName);
  153. >      cout<<"City : ";
  154. >      cout<<"Zip code : ";
  155. > }
  156.  
  157.